<template>
  <div class="resident-form">
    <h3 class="form-title">
      <el-icon><Edit /></el-icon>
      新建居民档案
    </h3>

    <el-divider />
    <div class="section-title">居民信息</div>

    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="form.name" placeholder="请输入" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="身份证号" prop="idCard">
            <el-input v-model="form.idCard" placeholder="请输入" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="性别" prop="gender">
            <el-select v-model="form.gender" placeholder="请选择" style="width: 100%">
              <el-option label="男" value="1" />
              <el-option label="女" value="0" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="出生年月" prop="birthdate">
            <el-date-picker v-model="form.birthdate" type="date" placeholder="请选择日期" style="width: 100%" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="form.phone" placeholder="请输入" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="家庭地址" prop="address">
            <el-input v-model="form.address" placeholder="请输入" />
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="居民标签" prop="tag">
            <el-select v-model="form.tag" placeholder="请选择" clearable style="width: 100%" multiple>
              <el-option v-for="item in tagOptions" :key="item.name" :label="item.name" :value="item.name" />
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="登录密码" prop="password">
            <el-input v-model="form.password" placeholder="8位以上的数字和字母组合" show-password />
          </el-form-item>
        </el-col>
      </el-row>

      <div class="form-buttons">
        <el-button type="primary" size="large" @click="handleSave">保存</el-button>
        <el-button type="primary" size="large" @click="goToInfoPage()">完善居民档案</el-button>
        <el-button type="success" size="large">立即签约</el-button>
        <el-button @click="goBack" size="large">返回</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import {onMounted, ref} from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Edit } from '@element-plus/icons-vue'
import axios from "axios";
import ResidentInfo from "./ResidentInfo.vue";

const router = useRouter()

const formRef = ref()

const form = ref({})

const tagOptions = ref('')

const goToInfoPage = () => {
  router.push('/main/ResidentInfo')
}

const getAllTag = () => {
  axios.get("/tag/getAllTag").then(resp => {
    tagOptions.value = resp.data;
  })
}

const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  idCard: [{ required: true, message: '请输入身份证号', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'change' }],
  phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入登录密码', trigger: 'blur' }]
}

const handleSave = () => {
  formRef.value.validate(valid => {
    if (valid) {
      axios.post(`/resident/addResident`, form.value).then(resp => {
        ElMessage.success(resp.data)
        router.push('/main/ResidentManagement')
      })
    }
  })
}

const goBack = () => {
  router.back()
}


onMounted(() => {
  getAllTag();
})
</script>

<style scoped>
.resident-form {
  padding: 32px;
  background-color: #fff;
  max-width: 1400px;
  margin: 0 auto;
}
.form-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.section-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 16px;
}
.form-buttons {
  margin-top: 24px;
  display: flex;
}
</style>
